package com.nxcy.student.controller;

import com.nxcy.common.bean.PagerInfo;
import com.nxcy.common.util.StringUtil;
import com.nxcy.student.bean.Student;
import com.nxcy.student.dao.StudentDao;
import com.nxcy.student.dao.StudentDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/student/list.do"})
public class ListStudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        StudentDao dao = new StudentDaoImpl();
        Student student = new Student();
        String pageNumStr = request.getParameter("pageNum");
        if (StringUtil.isEmpty(pageNumStr)) {
            student.getPager().setPageNum(1);
        } else {
            try {
                student.getPager().setPageNum(Integer.parseInt(pageNumStr));
            } catch (Exception e) {
                student.getPager().setPageNum(1);
            }
        }
        String sno = request.getParameter("sno");
        if (StringUtil.isNotEmpty(sno)) {
            student.setSno(sno);
        }

        String name = request.getParameter("name");
        if (StringUtil.isNotEmpty(name)) {
            student.setName(name);
        }

        String sex = request.getParameter("sex");
        if (StringUtil.isNotEmpty(sex)) {
            student.setSex(sex);
        }
        String phone = request.getParameter("phone");
        if (StringUtil.isNotEmpty(phone)) {
            student.setPhone(phone);
        }
        PagerInfo<Student> page = dao.queryStudentByPage(student);
        request.setAttribute("page", page);
        request.setAttribute("student", student);
        request.getRequestDispatcher("/java/05/index.jsp").forward(request, response);
    }
}
